#ifndef VIDEOVIEW_HXX
#define VIDEOVIEW_HXX

#include <QWidget>
#include <QTimer>
#include <QMouseEvent>

#include <vector>

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/core/ocl.hpp>


class Project;
class VideoController;

enum ViewStatus
{
	VW_STATUS_DISPLAYING,
	VW_STATUS_EDITING
};

class VideoView : public QWidget
{
    Q_OBJECT
public:
    explicit VideoView(QWidget *parent = nullptr);
    virtual void paintEvent(QPaintEvent *event);

	QRect cvRect2QRect(cv::Rect2d rect);
	cv::Rect2d qRect2cvRect(QRect rect);

	QPoint cvPoint2QPoint(cv::Point2d cpt);
	cv::Point2d qPoint2cvPoint(QPoint qpt);

	void drawTrackingObject(QPainter &painter);
	void drawNewObject(QPainter &painter);
	void drawTrackedObject(QPainter &painter);
	void drawSelectedObject(QPainter &painter, QRect &rect);
	void drawTextInfo(QPainter &painter);

	void reset();

signals:
	void frameInfoUpdated();
	void rectSelected();
	void objectSelected();
	void objectUnSelected();

public slots:
    void updateFrame();

public:
    inline void setProject(Project* project) { _project = project; }
	inline Project* getProject() { return _project; }
	inline void setController(VideoController* controller) { _controller = controller; }

	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *evet);
	void mouseDoubleClickEvent(QMouseEvent *event);

	void clearRects() { _rects.clear(); }

	inline std::vector<QRect> getRects() { return _rects; }
	std::vector<cv::Rect2d> getCvRects();

	inline int getCurrentFrameIndex() { return _frameIndex; }

private:
    uchar* _imageData;

    Project* _project;
	VideoController* _controller;

	QTimer* _timer;

	bool _dragingRect;
	QPoint _startPoint;
	QPoint _endPoint;

	std::vector<QRect> _rects;

	int _frameIndex;

	ViewStatus _status;
};

#endif // VIDEOVIEW_HXX
